var app = getApp();

Page({
  data: {
    carlist:[],
    editorshow:true,
    num:1,
    allCheck:false,
    total:0
  },
  editor:function(){
    this.setData({
      editorshow:!this.data.editorshow
    })
  },
  //获取input框中的值
  getNum:function(e){
    console.log(e)
    var index = e.target.dataset.index
    var carlist = this.data.carlist
    console.log(this.data.carlist)
  },
  // 失去焦点触发的事件
  blurnum:function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index
    var carlist = this.data.carlist
    carlist[index].num = e.detail.value;
    if(carlist[index].num <1){
      wx.showToast({
        title: '商品至少一个',
        icon:'none'
      })
      carlist[index].num = 1
      this.setData({
        carlist:carlist
      })
    }
    if(carlist[index].mostgt == 0){
      if(carlist[index].num > parseInt(carlist[index].snum)){
        wx.showToast({
          title: '您最多能买'+carlist[index].snum+'件商品',
          icon:'none'
        })
        carlist[index].num = carlist[index].snum
        this.getTotalPrice()
        this.setData({
          carlist:carlist
        })
      }
    }else{
      console.log(11111)
      if(carlist[index].num > parseInt(carlist[index].mostgt)){
        wx.showToast({
            title: '商品不能超过'+carlist[index].mostgt+'个',
            icon:'none'
          })
          carlist[index].num = carlist[index].mostgt
          this.getTotalPrice()
          this.setData({
            carlist:carlist
          })
      }
    }
    if(carlist[index].check == true){
      this.getTotalPrice()
    }
    
  },
  //单选按钮事件
  shopradio:function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index
    var carlist = this.data.carlist
    carlist[index].check = !carlist[index].check;
    this.setData({
      carlist:carlist
    })
    var arr = []
    for(var i=0;i<this.data.carlist.length;i++){
      if(this.data.carlist[i].check == true){
       arr.push(this.data.carlist[i])
      }
    }
    if(arr.length == carlist.length){
      this.setData({
        allCheck:true
      })
    }else{
      this.setData({
        allCheck:false
      })
    }
    this.getTotalPrice()
  },
  // 全选事件
  selectALL:function(e){
    var carlist = this.data.carlist
    this.setData({
      allCheck:!this.data.allCheck
    })
    if(this.data.allCheck){
      carlist.map((v,k)=>{
        v.check = true
      })
      this.setData({
        carlist:carlist
      })
    }else{
      carlist.map((v,k)=>{
        v.check= false
      })
      this.setData({
        carlist:carlist
      })
    }
    this.getTotalPrice()
  },
  // 加
  add:function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index
    var carlist = this.data.carlist
    
    if(carlist[index].check){
      carlist[index].num++;
      this.getTotalPrice()
    }else{
      carlist[index].num++;
    }
    if(carlist[index].mostgt == 0){
      if(carlist[index].num > parseInt(carlist[index].snum)){
        wx.showToast({
          title: '您最多能买'+carlist[index].snum+'件商品',
          icon:'none'
        })
        carlist[index].num = carlist[index].snum
        this.getTotalPrice()
        this.setData({
          carlist:carlist
        })
      }
    }else{
      console.log(11111)
      if(carlist[index].num > parseInt(carlist[index].mostgt)){
        wx.showToast({
            title: '商品不能超过'+carlist[index].mostgt+'个',
            icon:'none'
          })
          carlist[index].num = carlist[index].mostgt
          this.getTotalPrice()
          this.setData({
            carlist:carlist
          })
      }
    }
    
    this.setData({
      carlist:carlist
    })
   
  },
  // 减
  even:function(e){
    var index = e.currentTarget.dataset.index
    var carlist = this.data.carlist
    if(carlist[index].check){
      carlist[index].num--;
      if(carlist[index].num <1){
        wx.showToast({
          title: '至少一件商品',
          icon:"none"
        })
        carlist[index].num =1
      }
      this.getTotalPrice()
    }else{
      carlist[index].num--;
      if(carlist[index].num <1){
        wx.showToast({
          title: '至少一件商品',
          icon:"none"
        })
        carlist[index].num =1
      }
    }
    this.setData({
      carlist:carlist
    })
  },
  // 计算总价
  getTotalPrice() {
    let carlist = this.data.carlist;  // 获取购物车列表
    let total = 0;   
    for(let i=0;i<carlist.length;i++){
      if(carlist[i].check==true){
        total +=carlist[i].smoney * carlist[i].num
      }
      console.log(total)
    }   
    this.setData({
      total:total.toFixed(2)
    })
    console.log(total)
  },
  del:function(){
    var carlist = this.data.carlist
    console.log(carlist)
    var arr =[]
    var index = -1;
    var delNum  = 0;
    for(var i=0;i<carlist.length;i++){
      if(carlist[i].check){
      
      }else{
        arr.push(carlist[i]);
      }
    }
    console.log(arr)
    if(this.data.allCheck == true){
      arr=[]
    }
    this.setData({
      carlist:arr,
      allCheck:false,  
    })
    wx.setStorageSync('shopcar', this.data.carlist)
  },
  // 去选购
  xuangou:function(){
    wx.switchTab({
      url: '/hyb_yl/tabBar/shop/shop',
    
    })
  },
  goshop:function(){
    var carlist = this.data.carlist
    var arr=[]
    
    for(var i=0;i<carlist.length;i++){
      if(carlist[i].check == true){
        arr.push(carlist[i])
      }
    }
    if(arr.length == 0){
      wx.showToast({
        title: '您还没有选中商品',
        icon:"none"
      })
    }else{
      wx.navigateTo({
        url: '/hyb_yl/mysubpages/pages/shopdetail/shopdetail?arr='+ JSON.stringify(arr) + '&ifcfy='+this.data.ifcfy,
      })
    }
    console.log(arr)
   
  },
  onLoad: function(options) {
    console.log(options)
    this.getTotalPrice()
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    
    var carlist = wx.getStorageSync('shopcar')
    carlist.map((v,k)=>{
      console.log(v)
    })
    this.setData({
      carlist:carlist,
      ifcfy:options.ifcfy
    })
    console.log(this.data.carlist)
    this.selectALL()
  },
  onShow: function() {
   
  },
  onHide: function() {
   
  },
  onUnload: function() {
    console.log(this.data.carlist)
    wx.setStorageSync('shopcar', this.data.carlist)
  },
  
});